import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;


public class HttpTools {
    private static String filePath="D:/AppData/";

    public static String get(String urlStr){
        StringBuilder result=new StringBuilder();
        try{
            HttpURLConnection connection= (HttpURLConnection) new URL(urlStr).openConnection();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            int responseCode=connection.getResponseCode();
            if(responseCode==HttpURLConnection.HTTP_OK){
                InputStream inputStream=connection.getInputStream();
                String line=null;
                BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                while ((line=reader.readLine())!=null){
                    result.append(line);
                }
            }
            connection.disconnect();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

    public static boolean getFile(String urlStr){
        try {
            HttpURLConnection connection= (HttpURLConnection) new URL(urlStr).openConnection();
            connection.setConnectTimeout(60*1000);

            String filename=connection.getHeaderField("Content-Disposition");
            filename= URLDecoder.decode(filename.substring(filename.indexOf("filename=")+9),"utf-8");

            System.out.println("正在下载文件:"+filename);
            filename=filename.replace("\"","");
            InputStream inputStream=connection.getInputStream();
            File file=new File(filePath+filename);
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            byte[] bytes=new byte[1024];
            int len=0;
            while ((len=inputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
            if(fileOutputStream!=null){
                fileOutputStream.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String postFile(String urlStr, File file){
        String boundary="formBoundary";
        HttpURLConnection connection=null;
        String result="";
        try{
            URL url=new URL(urlStr);
            /**
             * 准备
             */
            connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(15*1000);
            connection.setReadTimeout(60*1000);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            connection.setRequestProperty("Charset","utf-8");

            /**
             * 开始
             */
            DataOutputStream dataOut=new DataOutputStream(connection.getOutputStream());

            byte[] end_data=("\r\n--"+boundary+"--\r\n").getBytes(StandardCharsets.UTF_8);

            StringBuilder form=new StringBuilder();

            form.append("--"+boundary);
            form.append("\r\n");
            form.append("Content-Disposition:form-data;name=\"file\";filename=\""+file.getName()+"\"\r\n");
            form.append("Content-Type:application/octet-stream\r\n\r\n");

            dataOut.write(form.toString().getBytes(StandardCharsets.UTF_8));

            DataInputStream fileInput=new DataInputStream(new FileInputStream(file));

            int len=0;
            byte[] bytes=new byte[1024];
            while ((len=fileInput.read(bytes))!=-1){
                dataOut.write(bytes,0,len);
            }
            fileInput.close();
            dataOut.write(end_data);
            dataOut.flush();
            dataOut.close();
            BufferedReader reader=new BufferedReader(new InputStreamReader(
                    connection.getInputStream()
            ));
            String line="";
            while ((line=reader.readLine())!=null){
                result+=line;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            connection.disconnect();
        }
        return result;
    }

}
